package edu.sjtu.erplab.cm.webservice.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import edu.sjtu.erplab.cm.business.bean.JpdlAndComponentXmlVB;
import edu.sjtu.erplab.cm.dao.bean.CollaborativeModule;
import edu.sjtu.erplab.cm.dao.impl.CollaborativeModuleDAO;
import edu.sjtu.erplab.cm.util.CollaborativeModuleConstant;
import edu.sjtu.erplab.cm.util.XMLParser;
import edu.sjtu.erplab.cm.webservice.interfaces.ICollaborativeModuleWebService;

@Service
public class CollaborativeModuleWebService implements
		ICollaborativeModuleWebService {

	@Autowired
	CollaborativeModuleDAO collaborativeModuleDAO;

	public String createOneCollaborativeModuleWith7Params(String id,
			String businessType, String updateType, String processKey,
			String jpdlXml, String componentsXml, String originalJpdl) {
		try {
			CollaborativeModule cm = collaborativeModuleDAO
					.createOneCollaborativeModuleWith7Params(id, businessType,
							updateType, processKey, jpdlXml, componentsXml,
							originalJpdl);
			if (cm != null) {
				return CollaborativeModuleConstant.CREATE_SUCCESS;
			} else {
				return CollaborativeModuleConstant.CREATE_IS_EXISTED;
			}

		} catch (RuntimeException re) {
			return CollaborativeModuleConstant.CREATE_FAiLURE;

		}
	}

	public String createSystemRecordByTemplateId(String templateId,
			String systemId) {
		try {
			CollaborativeModule cm = collaborativeModuleDAO.findBy2Prams(
					templateId, "template");
			if (cm != null) {
				CollaborativeModule newCm = collaborativeModuleDAO
						.createOneCollaborativeModuleWith7Params(systemId,
								"SYSTEM_SERVICE_UPDATEBYID", "system",
								cm.getProcessKey(), cm.getJpdlXml(),
								cm.getComponentsXml(), cm.getOriginalJpdl());
				if (newCm != null) {
					return CollaborativeModuleConstant.CREATE_SUCCESS;

				} else {
					return CollaborativeModuleConstant.CREATE_IS_EXISTED;
				}

			} else {
				return CollaborativeModuleConstant.FIND_FAiLURE;
			}
		} catch (RuntimeException re) {
			return CollaborativeModuleConstant.CREATE_FAiLURE;
		}
	}

	public String updateCollaborativeModuleBy2Params(String id,
			String updateType, String processKey, String jpdlXml,
			String componentsXml, String originalJpdl) {

		try {
			CollaborativeModule cm = collaborativeModuleDAO
					.updateCollaborativeModuleBy2Params(id, updateType,
							processKey, jpdlXml, componentsXml, originalJpdl);
			if (cm != null) {
				return CollaborativeModuleConstant.UPDATE_SUCCESS;
			} else {
				return CollaborativeModuleConstant.UPDATE_NOT_FINDED;
			}
		} catch (RuntimeException re) {
			return CollaborativeModuleConstant.UPDATE_FAiLURE;
		}
	}

	// 如果成功返回新的装配文件
	public String updateThreeFilesBy2Params(String id, String updateType,
			String changeXML) {
		try {
			CollaborativeModule cm = collaborativeModuleDAO.findBy2Prams(id,
					updateType);
			if (cm != null) {
				// 处理XML文件
				String oldJpdlXml = cm.getJpdlXml();
				String oldComponentsXml = cm.getComponentsXml();
				String oldOriginalJpdl = cm.getOriginalJpdl();
				if (oldJpdlXml != null && oldComponentsXml != null
						&& oldOriginalJpdl != null && !oldJpdlXml.isEmpty()
						&& !oldComponentsXml.isEmpty()
						&& !oldOriginalJpdl.isEmpty()) {
					XMLParser xmlParser = new XMLParser(changeXML);
					String newJpdlXml = xmlParser.changeJpdlXml(oldJpdlXml);
					String newComponentsXml = xmlParser
							.changeComponentsXml(oldComponentsXml);
					String newOriginalJpdl = xmlParser
							.changeOriginalJpdl(oldOriginalJpdl);
					cm.setJpdlXml(newJpdlXml);
					cm.setComponentsXml(newComponentsXml);
					cm.setOriginalJpdl(newOriginalJpdl);
					collaborativeModuleDAO.saveOrUpdate(cm);
					return newComponentsXml;

				} else {
					return CollaborativeModuleConstant.UPDATE_NOT_FINDED;
				}

			} else {
				return CollaborativeModuleConstant.UPDATE_NOT_FINDED;
			}
		} catch (RuntimeException re) {
			return CollaborativeModuleConstant.UPDATE_FAiLURE;
		}
	}

	public String findOriginalJpdlBy2Params(String id, String updateType) {
		String originalJpdl = "";
		try {
			CollaborativeModule cm = collaborativeModuleDAO.findBy2Prams(id,
					updateType);
			if (cm != null) {
				originalJpdl = cm.getOriginalJpdl();
				if (originalJpdl != null && !originalJpdl.equals(""))
					return originalJpdl;
			}
			return CollaborativeModuleConstant.FIND_FAiLURE;
		} catch (RuntimeException re) {
			return CollaborativeModuleConstant.FIND_FAiLURE;
		}

	}

	// 如果不存在或失败返回结果为null
	public JpdlAndComponentXmlVB findJpdlAndComponentXmlByProcessKey(
			String processKey) {
		JpdlAndComponentXmlVB jpdl_component = new JpdlAndComponentXmlVB();
		try {
			CollaborativeModule cm = collaborativeModuleDAO
					.findByProcessKey(processKey);
			if (cm != null) {
				jpdl_component.setProcessKey(processKey);
				jpdl_component.setJpdlXml(cm.getJpdlXml());
				jpdl_component.setComponentXml(cm.getComponentsXml());
				return jpdl_component;
			}
			return null;
		} catch (RuntimeException re) {
			return null;
		}
	}

	// 如果不存在或失败返回结果为null
	public CollaborativeModule findCollaborativeModuleBy2Params(String id,
			String updateType) {
		try {
			CollaborativeModule cm = collaborativeModuleDAO.findBy2Prams(id,
					updateType);
			return cm;
		} catch (RuntimeException re) {
			return null;
		}

	}

	// 如果不存在或失败返回结果为null
	@SuppressWarnings("unchecked")
	public List<CollaborativeModule> findAllCollaborativeModule() {
		try {
			List<CollaborativeModule> list = collaborativeModuleDAO.findAll();
			if (list != null && list.size() > 0)
				return list;
			else
				return null;
		} catch (RuntimeException re) {
			return null;
		}

	}

	public String deleteCollaborativeModuleBy2Params(String id,
			String updateType) {
		try {
			return collaborativeModuleDAO.deleteCollaborativeModuleBy2Params(
					id, updateType);
		} catch (RuntimeException re) {
			return CollaborativeModuleConstant.DELETE_FAiLURE;
		}

	}

	// //////////////////////////////////////////////////////
	// TODO
	/*
	 * public String createOneCollaborativeModuleWith3Params(String templateId,
	 * String businessType, String updateType) { CollaborativeModule cm = new
	 * CollaborativeModule(); cm.setId(templateId);
	 * cm.setBusinessType(businessType); cm.setUpdateType(updateType); try {
	 * collaborativeModuleDAO.save(cm); return
	 * CollaborativeModuleConstant.CREATE_SUCCESS; } catch (RuntimeException re)
	 * { return CollaborativeModuleConstant.CREATE_FAiLURE; } }
	 * 
	 * public String updateCollaborativeModuleByTemplateId(String templateId,
	 * String processKey, String jpdlXml, String componentsXml, String
	 * originalJpdl) { try { CollaborativeModule cm = collaborativeModuleDAO
	 * .updateCollaborativeModuleByTemplateId(templateId, processKey, jpdlXml,
	 * componentsXml, originalJpdl); if (cm != null) { return
	 * CollaborativeModuleConstant.UPDATE_SUCCESS; } else { return
	 * CollaborativeModuleConstant.UPDATE_NOT_FINDED; } } catch
	 * (RuntimeException re) { return
	 * CollaborativeModuleConstant.UPDATE_FAiLURE; }
	 * 
	 * }
	 * 
	 * public String updateCollaborativeModuleBy3Params(String templateId,
	 * String businessType, String updateType, String processKey, String
	 * jpdlXml, String componentsXml, String originalJpdl) {
	 * 
	 * try { CollaborativeModule cm = collaborativeModuleDAO
	 * .updateCollaborativeModuleBy3Params(templateId, businessType, updateType,
	 * processKey, jpdlXml, componentsXml, originalJpdl); if (cm != null) {
	 * return CollaborativeModuleConstant.UPDATE_SUCCESS; } else { return
	 * CollaborativeModuleConstant.UPDATE_NOT_FINDED; } } catch
	 * (RuntimeException re) { return
	 * CollaborativeModuleConstant.UPDATE_FAiLURE; } }
	 * 
	 * public String findOriginalJpdlByTemplateId(String templateId) { String
	 * originalJpdl = ""; try { CollaborativeModule cm = collaborativeModuleDAO
	 * .findByTemplateId(templateId); if (cm != null) { originalJpdl =
	 * cm.getOriginalJpdl(); if (originalJpdl != null &&
	 * !originalJpdl.equals("")) return originalJpdl; } return
	 * CollaborativeModuleConstant.FIND_FAiLURE; } catch (RuntimeException re) {
	 * return CollaborativeModuleConstant.FIND_FAiLURE; }
	 * 
	 * }
	 * 
	 * // 如果不存在或失败返回结果为null public CollaborativeModule
	 * findCollaborativeModuleByTemplateId( String templateId) { try {
	 * CollaborativeModule cm = collaborativeModuleDAO
	 * .findByTemplateId(templateId); return cm; } catch (RuntimeException re) {
	 * return null; }
	 * 
	 * }
	 */

}
